package src.util;

import java.util.ArrayList;

import org.apache.log4j.Logger;


public class Message
{
	// ---------------------------------------------------------------------------------------------
	private Logger log = Logger.getLogger(Message.class);
	private static MessageCfg			messageCfg		= null;

	@SuppressWarnings("rawtypes")
	private Class							messageClass	= null;

	private int								status			= 0;
	private String							statusInfo		= "";
	private long							length			= 0;
	private String							checkSum			= "";

	private String							request			= null;
	private ArrayList<Messageitem>	list				= new ArrayList<Messageitem>( );
	private StringBuffer					responseData	= new StringBuffer( );
	private int								ctrLine			= 0;

	// ---------------------------------------------------------------------------------------------

	public Message( MessageCfg messageCfg)
	{
		Message.messageCfg = messageCfg;

		new Messageitem( messageCfg);
	}

	// ---------------------------------------------------------------------------------------------

	public Message( String request)
	{
		this( request, null);
	}

	// ---------------------------------------------------------------------------------------------

	public Message( )
	{
		this( "", null);
	}

	// ---------------------------------------------------------------------------------------------

	public static MessageCfg getMessageCfg()
	{
		return messageCfg;
	}
	
	// ---------------------------------------------------------------------------------------------

	@SuppressWarnings("rawtypes")
	public Message( String request, Class messageClass)
	{
		this.request = request;
		this.messageClass = messageClass;
	}

	// ---------------------------------------------------------------------------------------------

	public String getRequest( )
	{
		return request;
	}

	// ---------------------------------------------------------------------------------------------

	public void setRequest( String request)
	{
		this.request = request;
	}

	// ---------------------------------------------------------------------------------------------

	public long getStatus( )
	{
		return status;
	}

	// ---------------------------------------------------------------------------------------------

	public void setStatus( int status)
	{
		this.status = status;
	}

	// ---------------------------------------------------------------------------------------------

	public String getStatusInfo( )
	{
		return statusInfo;
	}

	// ---------------------------------------------------------------------------------------------

	public void setStatusInfo( String statusInfo)
	{
		this.statusInfo = statusInfo;
	}

	// ---------------------------------------------------------------------------------------------

	public long length( )
	{
		return length;
	}

	// ---------------------------------------------------------------------------------------------

	public String getCheckSum( )
	{
		return checkSum;
	}


	// ---------------------------------------------------------------------------------------------

	public void addXmlParam( Object object)
	{
		if( object != null)
		{
//			list.add( new Messageitem( "xml-" + object.getClass( ).getSimpleName( ), XmlTool.toXML( object)));
		}
		else
		{

		}
	}

	// ---------------------------------------------------------------------------------------------

	public void addStringParam( String value)
	{
		addParam( "string", value);
	}

	// ---------------------------------------------------------------------------------------------

	public void addParam( String name, String value)
	{
		list.add( new Messageitem( name, value));
	}

	// ---------------------------------------------------------------------------------------------

	public String getRequestData( )
	{
		StringBuffer requestData = new StringBuffer( );

		for( int idx = 0; idx < list.size( ); idx ++ )
		{
			if( requestData.length( ) > 0)
			{
				requestData.append( "&");
			}

			requestData.append( list.get( idx).toString( ));
		}

		String data = requestData.toString( );
		length = data.length( );
		if( messageCfg.isCheckSumVerify( ))
		{
			checkSum = Checksum.get( data, messageCfg.getCheckSumAlgo( ));
		}
		else
		{
			checkSum = "None";
		}

		return "length=" + length + "&checksum=" + checkSum + "&" + data;
	}

	// ---------------------------------------------------------------------------------------------
	// ---------------------------------------------------------------------------------------------

	public void appendLine( String line)
	{
		switch( ctrLine)
		{
			case 0:
				String statusStr[] = line.split( " ");
				status = Integer.parseInt( statusStr[0]);
				if( statusStr.length > 1)
				{
					statusInfo = statusStr[1];
				}
				ctrLine ++ ;
				break;

			case 1:
				length = Long.parseLong( line);
				ctrLine ++ ;
				break;

			case 2:
				checkSum = line;
				ctrLine ++ ;
				break;

			default:
				responseData.append( line);
				break;
		}
	}

	// ---------------------------------------------------------------------------------------------

	public boolean check( )
	{
		return length == responseData.length( ) && checkSum.equals( Checksum.get( responseData.toString( )));
	}

	// ---------------------------------------------------------------------------------------------

	public String getResponseData( )
	{
		return responseData.toString( );
	}

	// ---------------------------------------------------------------------------------------------

	public Object convert( )
	{
		if( responseData.length( ) != 0)
		{
			try
			{
//				return XmlTool.fromXML( responseData.toString( ), messageClass);
				return responseData.toString();
			}
			catch( Exception ex)
			{

			}
		}
		return null;
	}

	// ---------------------------------------------------------------------------------------------

	public String toClient( long status, String statusInfo)
	{
		String data = "";
		if( list.size( ) > 0)
		{
			data = list.get( 0).getData( );
			if( messageCfg.isCheckSumVerify( ))
			{
				checkSum = Checksum.get( data, messageCfg.getCheckSumAlgo( ));
			}
		}
		length = data.length( );
		return "" + status + " " + statusInfo + "\n" + length + "\n" + checkSum + "\n" + data;
	}

	// ---------------------------------------------------------------------------------------------

	public String toServer( )
	{
		return "request=" + request + "\n" + getRequestData( );
	}

	// ---------------------------------------------------------------------------------------------

	public String toString( )
	{
		return "responseData=" + responseData;
	}

	// ---------------------------------------------------------------------------------------------
}

